home *** CD-ROM | disk | FTP | other *** search
- /*
- * offScreenBitMap.c Copyright © 1990 Brigham Stevens
- * -----------------
- *
- * This file contains routines for working with off screen bitmaps.
- *
- * NewBitMap - Allocates an off screen BitMap from a given Rect.
- * CalcOffScreen - Calculates fields of a BitMap for a given Rect.
- * FreeBitMap - Frees allocated memory in an off screen BitMap.
- * DrawBitMap - Draw a BitMap in the current GrafPort.
- * LoadPicture - Makes an off screen BitMap from a PICT resource.
- *
- */
-
- #include "offScreenBitMap.h"
-
- /*------------------------------------------------------------------------------
- * NewBitMap
- * Allocates an off Screen bitmap with the portBits of frame.
- * The BitMap is returned with all the fields initialized. Check MemErr
- * after calling NewBitMap to make sure the memory was allocated.
- *
- *------------------------------------------------------------------------------*/
-
- void NewBitMap(frame,theMap)
- Rect *frame;
- BitMap *theMap;
- {
- int size;
- int rbytes;
- CalcOffScreen(frame,&size,&rbytes);
- theMap->rowBytes=rbytes;
- theMap->bounds=*frame;
- theMap->baseAddr=NewPtr(size);
- }
-
- /*------------------------------------------------------------------------------
- * CalcOffScreen
- * Calculate the number of bytes needed and the
- * rowBytes field for a BitMap that has the portBits of frame.
- *
- *------------------------------------------------------------------------------*/
-
- void CalcOffScreen(frame,needed,rows)
- register Rect *frame;
- register int *needed;
- register int *rows;
- {
- *rows=((((frame->right) - (frame->left)) + 15)/16) *2;
- *needed=((*rows) * ((frame->bottom) - (frame->top)));
- }
-
-
- /*------------------------------------------------------------------------------
- * FreeBitMap
- * Deallocates an offscreen BitMap and sets all the fields
- * to Zero. Call when you are completely done with a BitMap that was
- * allocated with NewBitMap.
- *
- *------------------------------------------------------------------------------*/
-
- void FreeBitMap(Bits)
- BitMap *Bits;
- {
- DisposPtr(Bits->baseAddr);
- Bits->baseAddr=NIL;
- SetRect(&Bits->bounds,0,0,0,0);
- Bits->rowBytes=0;
- }
-
- /*------------------------------------------------------------------------------
- * DrawBitMap
- * This will Draw the BitMap passed to in the current grafPort
- * in rectangle frame using the bit Transfer mode passed.
- *
- *------------------------------------------------------------------------------*/
-
- void DrawBitMap(bits,frame,mode)
- BitMap *bits;
- Rect *frame;
- int mode;
- {
- CopyBits(bits, /* from bitmap */
- &(thePort->portBits), /* to bitmap */
- &(bits->bounds), /* from rect */
- frame, /* to rect */
- mode, /* transfer mode */
- NIL); /* Mask Region */
- }
-
-
-
- /*------------------------------------------------------------------------------
- * LoadPicture
- * Creates an offscreen bitmap and draws the picture
- * with the passed resID into it. Then the pict is deallocated.
- * be sure to call SetPort after calling this routine, because
- * thePort is invalid when it exits. If the memory for the off screen
- * bitmap cannot be allocated then the routine cleans up and exits, setting
- * the BaseAddr field of the BitMap to zero.
- *
- *------------------------------------------------------------------------------*/
-
- void LoadPicture(resID,theMap)
- int resID;
- BitMap *theMap;
- {
- PicHandle pict;
- Rect r;
- GrafPort tempPort;
-
- pict=GetPicture(resID);
- if(ResError())
- {
- theMap->baseAddr=NIL;
- return;
- }
- r=(**pict).picFrame;
-
- NewBitMap(&r,theMap);
- if(MemErr)
- {
- theMap->baseAddr=NIL;
- DisposHandle(pict);
- return;
- }
-
- OpenPort(&tempPort);
- SetPort(&tempPort);
- SetPortBits(theMap);
- FillRect(&tempPort.portRect,white);
- DrawPicture(pict,&r);
- DisposHandle(pict);
- ClosePort(&tempPort);
- }
-